/*
 *@author: YangJie
 *@date: 2021-04-16 16:12:10
 *@version:
 *@description:参数设置->设备参数设置->编码规则
 */
import React from 'react';
import { Tabs, Table, Input, Button, Form, notification } from 'antd';
import '@/assets/css/settings';
import { euqApplicationName } from '../../../config';
const { TabPane } = Tabs;

function openNotification(type, message) {
	notification.open({
		type: type,
		message: message,
		duration: 2,
	});
};
class EncodingRules extends React.Component {
	state = {
		loading: false, //加载中...
		loading_file: false,
		dataSource: [],
		datatestDetails: [],
		visible: false, //显示与隐藏
		btnDrawer: false,
		btnDrawer_file: false,
		modalData: '',//选中的数据
		handleType: 0,
		pagination: {
			pageSize: 10,
			current: 1
		},
		visible2: false,
		tabKey: '1',
		itemType: 1,
		chouyangType: '2',
		reportRepairNoDataSource: [],
		equipmentDataSource: [],
		repairDataSource: [],
		maintainDataSource: [],
	};
	componentDidMount() {
		this.fetch()
	}
	//初始化加载数据
	fetch = data => {
		this.setState({ loading: true })
		this.get(euqApplicationName + '/api/findCodeRulesPageList?pageSize=9999&pageNum=1').then(res => {
			console.log(res.data.data)
			let data = res.data.data
			let reportRepairNoDataSource = []
			let equipmentDataSource = []
			let repairDataSource = []
			let maintainDataSource = []
			for (const item of data) {
				if (item.type === 2) {
					reportRepairNoDataSource.push(item)
					this.setState({ reportRepairNoDataSource })
				} else if (item.type === 1) {
					equipmentDataSource.push(item)
					this.setState({ equipmentDataSource })
				} else if (item.type === 3) {
					repairDataSource.push(item)
					this.setState({ repairDataSource })
				} else if (item.type === 4) {
					maintainDataSource.push(item)
					this.setState({ maintainDataSource })
				}
			}
			console.log(this.state)
			// this.setState({
			// 	loading: false,
			// 	dataSource: res.data.data,
			// 	total:res.data.total,
			// 	formValues:data
			// });
		})
	};

	saves = () => {
		// console.log(this.state.reportRepairNoDataSource)
		// console.log(this.state.equipmentDataSource)
		// console.log(this.state.maintainDataSource)
		// console.log(this.state.repairDataSource)
		let data = []
		data.push.apply(data, this.state.reportRepairNoDataSource)
		data.push.apply(data, this.state.equipmentDataSource)
		data.push.apply(data, this.state.maintainDataSource)
		data.push.apply(data, this.state.repairDataSource)
		console.log(data)
		this.post(euqApplicationName + '/api/addCodeRulesList', { data }).then(res => {
			openNotification('success', '保存成功！')
			this.fetch()
		})
	}

	input1 = (e,record,index) => {
		record.textRules = e.target.value
		console.log(e.target.value,record,index)
	 }
	render() {
		const columns = [{
			key: 'codeRules',
			title: '编码规则',
			dataIndex: 'codeRules',
			width: '40%',
		},
		{
			key: 'textRules',
			title: '规则内容',
			width: '40%',
			dataIndex: 'textRules',
			render:(text,record,index)=>{
				return <Input  defaultValue={text ? text : ''} onBlur={(e)=>this.input1(e,record,index)} onPressEnter={(e)=>this.input1(e,record,index)}/>
			}
		}]
		const { reportRepairNoDataSource, equipmentDataSource, repairDataSource, maintainDataSource } = this.state;

		return (
			<div className="shadow-radius">
				<Tabs>
					<TabPane tab="编码规则" key="1">
						<div className="content-title">设备编码规则</div>
						<Table rowClassName={() => 'editable-row'} bordered dataSource={equipmentDataSource} columns={columns} pagination={false} style={{ width: '50%' }} />
						{/* <Button icon="plus" onClick={() => this.handleAdd('REPORT')} type="primary">
							添加
						</Button> */}
						<div className="content-title">报修单号规则</div>
						<p>注：单号规则中可以只使用部分前缀，如只用日期+数字，或只用数字！</p>
						<p>日期前缀中yyyy为年，MM为月，dd为日，数字前缀中000为当日自增序号位数为3位</p>
						{/* <Button icon="plus" onClick={() => this.handleAdd('REPORT')} type="primary">
							添加
						</Button> */}
						<Table rowClassName={() => 'editable-row'} bordered dataSource={reportRepairNoDataSource} columns={columns} pagination={false} style={{ width: '50%' }} />
						<div className="content-title">维修单号规则</div>
						{/* <Button icon="plus" onClick={() => this.handleAdd('REPAIR')} type="primary">
							添加
						</Button> */}
						<Table rowClassName={() => 'editable-row'} bordered dataSource={repairDataSource} columns={columns} pagination={false} style={{ width: '50%' }} />
						<div className="content-title">保养单号规则</div>
						{/* <Button icon="plus" onClick={() => this.handleAdd('MAINTAIN')} type="primary">
							添加
						</Button> */}
						<Table rowClassName={() => 'editable-row'} bordered dataSource={maintainDataSource} columns={columns} pagination={false} style={{ width: '50%' }} />
						<Button type="primary" style={{ margin: '1% 0 0 21%' }} onClick={() => { this.saves() }}>
							保存
						</Button>
					</TabPane>
				</Tabs>
			</div>
		);
	}
}

export default Form.create()(EncodingRules);
